package com.gxa.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.common.uitls.R;
import com.gxa.dto.CompanyDto;
import com.gxa.entity.Company;
import com.gxa.service.CompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "公司架构接口")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    @PostMapping("/com/list")
    @ApiOperation("查询所有公司,并按公司名字查询")
    public R queryAll(@RequestBody CompanyDto companyDto) {
        try {
            // PageHelper.startPage(page, limit);
            Page<Map<String,Object>> pageHelper = PageHelper.startPage(companyDto.getPage(), companyDto.getLimit());
            List<Company> companies = this.companyService.queryAll(companyDto);
            Map<String,Object> map=new HashMap<>();
            map.put("companies",companies );
            map.put("count",pageHelper.getTotal());
            return R.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }

    }

    @PostMapping("/com/add")
    @ApiOperation("添加公司接口")

    public R addCompany( @RequestBody Company company) {

        try {

            this.companyService.add(company);
            return R.ok("添加成功");


        } catch (Exception e) {
            e.printStackTrace();
            return R.error("添加失败");
        }

    }

    @PostMapping("/com/updateCompany")
    @ApiOperation("编辑操作")
    public R updateCompany( @RequestBody Company company ) {
        try {
            this.companyService.updateCompany(company);
            return R.ok("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return R.error("修改失败");
        }

    }


}

